﻿using Nop.Admin.Models.Home;
using Nop.Core;
using Nop.Core.Caching;
using Nop.Core.Domain.Common;
using Nop.Core.Domain.Customers;
using Nop.Services.Configuration;
using Nop.Services.Customers;
using Nop.Services.Directory;
using Nop.Services.Security;
using Nop.Services.XJ;
using System.Web.Mvc;

namespace Nop.Admin.Controllers
{
	public partial class HomeController : BaseAdminController
	{
		#region Fields
		private readonly IStoreContext _storeContext;
		private readonly AdminAreaSettings _adminAreaSettings;
		private readonly ISettingService _settingService;
		private readonly IPermissionService _permissionService;
		private readonly IDepartmentService _departmentService;
		private readonly ICustomerService _customerService;
		private readonly IXJMaintenanceService _xJMaintenanceService;
		private readonly IXJInspectionService _xJInspectionService;
		private readonly IXJEquipmentStockInService _xJEquipmentStockInService;
		private readonly IXJEquipmentStockInDetailService _xJEquipmentStockInDetailService;
		private readonly IXJEquipmentStockOutService _xJEquipmentStockOutService;
		private readonly IXJEquipmentStockOutDetailService _xJEquipmentStockOutDetailService;
		private readonly IWorkContext _workContext;
		private readonly ICacheManager _cacheManager;
		private readonly IWebHelper _webHelper;

		#endregion

		#region Ctor

		public HomeController(IStoreContext storeContext,
			AdminAreaSettings adminAreaSettings,
			ISettingService settingService,
			IPermissionService permissionService,
			IDepartmentService departmentService,
			ICustomerService customerService,
			IXJMaintenanceService xJMaintenanceService,
			IXJInspectionService xJInspectionService,
			IXJEquipmentStockInService xJEquipmentStockInService,
			IXJEquipmentStockInDetailService xJEquipmentStockInDetailService,
			IXJEquipmentStockOutService xJEquipmentStockOutService,
			IXJEquipmentStockOutDetailService xJEquipmentStockOutDetailService,
			IWorkContext workContext,
			ICacheManager cacheManager,
			IWebHelper webHelper)
		{
			this._storeContext = storeContext;
			this._adminAreaSettings = adminAreaSettings;
			this._settingService = settingService;
			this._permissionService = permissionService;
			this._departmentService = departmentService;
			this._customerService = customerService;
			this._xJMaintenanceService = xJMaintenanceService;
			this._xJInspectionService = xJInspectionService;
			this._xJEquipmentStockInService = xJEquipmentStockInService;
			this._xJEquipmentStockInDetailService = xJEquipmentStockInDetailService;
			this._xJEquipmentStockOutService = xJEquipmentStockOutService;
			this._xJEquipmentStockOutDetailService = xJEquipmentStockOutDetailService;
			this._workContext = workContext;
			this._cacheManager = cacheManager;
			this._webHelper = webHelper;
		}

		#endregion

		#region Methods

		public virtual ActionResult Index()
		{
			var model = new DashboardModel();
			return View(model);
		}

		[ChildActionOnly]
		public virtual ActionResult CommonStatistics()
		{
			var model = new CommonStatisticsModel();

			var customer = _workContext.CurrentCustomer;
			if (null == customer)
				return PartialView(model);

			if (_permissionService.Authorize(StandardPermissionProvider.ManageMaintenances))
			{
				model.CanManage = true;
				model.CanManageMaintenances = true;
				model.NumberOfMantance = _xJMaintenanceService.GetMaintenances(customer: customer, pageSize: 1).TotalCount;
			}
			else if (_permissionService.Authorize(StandardPermissionProvider.ManageMyMaintenances))
			{
				model.CanManage = true;
				model.CanManageMyMaintenances = true;
				model.NumberOfMantance = _xJMaintenanceService.GetMaintenances(customer: customer, pageSize: 1).TotalCount;
			}
			if (_permissionService.Authorize(StandardPermissionProvider.ManageInspectionsAll))
			{
				model.CanManage = true;
				model.CanManageInspectionsAll = true;
				model.NumberOfInspection = _xJInspectionService.GetAllInspections(pageSize: 1).TotalCount;
			}
			else if (_permissionService.Authorize(StandardPermissionProvider.ManageInspections))
			{
				model.CanManage = true;
				model.CanManageInspections = true;
				model.NumberOfInspection = _xJInspectionService.GetMyInspections(customer: customer, pageSize: 1).TotalCount;
			}
			if (_permissionService.Authorize(StandardPermissionProvider.ManageOutsAudit))
			{
				model.CanManage = true;
				model.CanManageOutsAudit = true;
				model.NumberOfEquipmentStockOut = _xJEquipmentStockOutDetailService.GetEquipmentStockOutDetails(pageSize: 1).TotalCount;
			}
			else if (_permissionService.Authorize(StandardPermissionProvider.ManageOuts))
			{
				if (customer.IsAdmin() || customer.IsKeyLeader())
				{
					model.CanManage = true;
					model.CanManageOutsAudit = true;
					model.NumberOfEquipmentStockOut = _xJEquipmentStockOutDetailService.GetEquipmentStockOutDetails(pageSize: 1).TotalCount;
				}
				else
				{
					model.CanManage = true;
					model.CanManageOuts = true;
					model.NumberOfEquipmentStockOut = _xJEquipmentStockOutDetailService.GetEquipmentStockOutDetails(customerId: customer.Id, pageSize: 1).TotalCount;
				}
			}
			if (_permissionService.Authorize(StandardPermissionProvider.ManageIns))
			{
				model.CanManage = true;
				model.CanManageIns = true;
				model.NumberOfEquipmentStockIn = _xJEquipmentStockInDetailService.GetEquipmentStockInDetails(pageSize: 1).TotalCount;
			}

			return PartialView(model);
		}

		#endregion
	}
}
